<!-- src/views/lawyer/LawyerInfo.vue -->
<script setup>
import { onMounted, ref } from "vue";
import { ElMessage } from 'element-plus';
import { LawyerStores } from '@/stores/LawyerStores';
import LawyerInfoEdit from './LawyerInfoEdit.vue';
import LawyerInfoDetail from './LawyerInfoDetail.vue';

const lawyerStore = LawyerStores();

const tableData = ref([]);
const searchForm = ref({
  licenseNo: ''  // 修改字段名以匹配后端参数
});

const loading = ref(false);
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);

const dialogVisible = ref(false);
const detailDialogVisible = ref(false);
const currentLawyer = ref({});

const fetchData = async () => {
  try {
    loading.value = true;
    // 确保参数名与后端API匹配
    const params = {
      page: currentPage.value,
      size: pageSize.value,
      licenseNo: searchForm.value.licenseNo || undefined  // 空字符串时传undefined
    };

    console.log('查询参数:', params);

    await lawyerStore.pagelist(params);

    tableData.value = lawyerStore.lawyerList;
    total.value = lawyerStore.pageInfo.total;

    console.log('查询结果:', lawyerStore.lawyerList);

  } catch (error) {
    ElMessage.error('获取律师列表失败: ' + (error.message || '未知错误'));
    console.error('API Error:', error);
    tableData.value = [];
    total.value = 0;
  } finally {
    loading.value = false;
  }
};

const handleSizeChange = (val) => {
  pageSize.value = val;
  currentPage.value = 1;
  fetchData();
};

const handleCurrentChange = (val) => {
  currentPage.value = val;
  fetchData();
};

const resetSearch = () => {
  searchForm.value = {
    licenseNo: ''
  };
  currentPage.value = 1;
  fetchData();
};

const handleEdit = (row) => {
  currentLawyer.value = { ...row };
  dialogVisible.value = true;
};

// 查看详情功能
const handleViewDetails = (row) => {
  currentLawyer.value = { ...row };
  detailDialogVisible.value = true;
};

const handleSuccess = () => {
  fetchData();
};

// 页面加载时获取数据
onMounted(() => {
  fetchData();
});
</script>

<template>
  <div class="lawyer-management-container">
    <el-card shadow="hover" class="search-card">
      <div class="search-header">
        <h3>律师查询</h3>
        <div class="search-actions">
          <el-button type="primary" @click="fetchData" :loading="loading">
            <el-icon class="el-icon--left">
              <Search />
            </el-icon>
            查询
          </el-button>
          <el-button @click="resetSearch">
            <el-icon class="el-icon--left">
              <Refresh />
            </el-icon>
            重置
          </el-button>
        </div>
      </div>

      <el-form :model="searchForm" label-width="100px" class="search-form">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="执业证号">
              <el-input
                  v-model="searchForm.licenseNo"
                  placeholder="请输入执业证号"
                  clearable
                  @keyup.enter="fetchData"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <el-card shadow="hover" class="table-card">
      <el-table
          :data="tableData"
          style="width: 100%"
          v-loading="loading"
          stripe
          border
          highlight-current-row
      >
        <el-table-column label="ID" prop="id" width="80" align="center" />
        <el-table-column label="关联用户ID" prop="userId" width="120" align="center" />
        <el-table-column label="执业证号" prop="licenseNo" width="150" align="center" />
        <el-table-column label="服务区域" prop="serviceArea" width="150" align="center" />
        <el-table-column label="专业领域" prop="specialty" width="150" align="center" />
        <el-table-column label="执业年限" prop="yearsOfPractice" width="100" align="center" />
        <el-table-column label="案件成功率" prop="successRate" width="120" align="center"/>
        <el-table-column label="个人介绍" prop="introduction" width="180" align="center" />
        <el-table-column label="操作" width="180" fixed="right" align="center">
          <template #default="scope">
            <div class="action-buttons">
              <el-button type="primary" size="small" @click="handleEdit(scope.row)">
                编辑
              </el-button>
              <el-button type="info" size="small" @click="handleViewDetails(scope.row)">
                详情
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
            background
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            :current-page="currentPage"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 编辑弹窗 -->
    <LawyerInfoEdit
        v-model:visible="dialogVisible"
        :lawyer-data="currentLawyer"
        @success="handleSuccess"
    />

    <!-- 详情弹窗 -->
    <LawyerInfoDetail
        v-model:visible="detailDialogVisible"
        :lawyer-data="currentLawyer"
    />
  </div>
</template>

<style scoped>
.lawyer-management-container {
  padding: 20px;
}

.search-card {
  margin-bottom: 20px;
  border-radius: 8px;
}

.search-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.search-header h3 {
  margin: 0;
  color: #409EFF;
  font-size: 18px;
}

.search-form {
  margin-top: 10px;
}

.table-card {
  border-radius: 8px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-table {
  font-size: 14px;
}

.el-table :deep(.el-table__cell) {
  padding: 12px 0;
}

.action-buttons {
  display: flex;
  justify-content: center;
  gap: 8px;
}

.el-button {
  padding: 8px 15px;
}
</style>
